package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jay
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息dish
        this.save(dishDto);

        //判断口味是否为空
        if(dishDto.getFlavors() == null){
            return;
        }
        //获取菜品id
        Long dishId = dishDto.getId();
        //赋值到菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors(); //拿到口味集合
        for (int i = 0; i < flavors.size(); i++) {
            flavors.get(i).setDishId(dishId);
        }
        //保存菜品口味数据dishFlavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 按id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        //查询对应的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
        //条件过滤
        dishFlavorWrapper.eq(DishFlavor::getDishId, id);
        //查询
        List<DishFlavor> list = dishFlavorService.list(dishFlavorWrapper);

        //创建dishDto对象,并赋值
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto); //数据复制
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品和菜品口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //菜品基本信息修改
        this.updateById(dishDto);

        //口味信息修改1. 先清理
        LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
        //获取菜品id
        Long dishId = dishDto.getId();
        dishFlavorWrapper.eq(DishFlavor::getDishId,dishId);
        dishFlavorService.remove(dishFlavorWrapper);
        //2. 后添加
        //判断口味是否为空
        if(dishDto.getFlavors().size() == 0 || dishDto.getFlavors() == null){
            return;
        }
        //赋值到菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors(); //拿到口味集合
        for (int i = 0; i < flavors.size(); i++) {
            flavors.get(i).setDishId(dishId);
        }
        //保存菜品口味数据dishFlavor
        dishFlavorService.saveBatch(flavors);
    }
}
